package com.su.czl.service.impl;

import com.su.czl.entity.Dictionarys;
import com.su.czl.mapper.DictMapper;
import com.su.czl.service.DictService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.List;

@Service
public class DictServiceImpl implements DictService{
    @Resource
    private DictMapper dictMapper;
    @Override
    public List<Dictionarys> selects() {
        List<Dictionarys> list = dictMapper.selects();
        return list;
    }

    @Override
    public List<Dictionarys> selectDictsInRange(String dict_name) {
        List<Dictionarys> dictionarys = dictMapper.selectDictsInRange(dict_name);
        return dictionarys;
    }

    @Override
    public Integer addDict(String dict_name, String dict_type, String remark) {
        Integer i = dictMapper.addDict(dict_name, dict_type, remark);
        return i;
    }

    @Override
    public Integer updataDict(Integer id, String dame, String type, String remark) {
        String s = dictMapper.selectById(id);
        if(s.equals("禁用")){
            return -1;
        }else {
            Integer i = dictMapper.updataDict(id, dame, type, remark);
            return i;   
        }
    }

    @Override
    public Integer deleteById(Integer id) {
        String s = dictMapper.selectById(id);

        if(s.equals("禁用")){
            return -1;
        }else {
            Integer i = dictMapper.deleteById(id);
            return i;
        }
    }

    @Override
    public Integer deleteDictionaries(List<Integer> ids) {
        Integer i = dictMapper.deleteDictionaries(ids);
        return i;
    }

    @Override
    public void exportDictsToExcel(List<Dictionarys> dictionarys, HttpServletResponse response) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook(); // 创建工作簿
        Sheet sheet = workbook.createSheet("字典数据"); // 创建工作表

        // 创建标题行
        Row titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("ID");
        titleRow.createCell(1).setCellValue("字典名称");
        titleRow.createCell(2).setCellValue("类型");
        titleRow.createCell(3).setCellValue("状态");
        titleRow.createCell(4).setCellValue("备注");
        titleRow.createCell(5).setCellValue("创建时间");

        // 写入数据
        for (int i = 0; i < dictionarys.size(); i++) {
            Dictionarys dictionaryss = dictionarys.get(i);
            Row row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(dictionaryss.getId().toString());
            row.createCell(1).setCellValue(dictionaryss.getDict_name());
            row.createCell(2).setCellValue(dictionaryss.getDict_type());
            row.createCell(3).setCellValue(dictionaryss.getSataus());
            row.createCell(4).setCellValue(dictionaryss.getRemark());
            row.createCell(5).setCellValue(dictionaryss.getCreated_at().toString());
        }

        // 设置响应的头信息
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = "DictData.xlsx"; // 文件名
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

        // 写入Excel文件到响应输出流
        try (OutputStream outputStream = response.getOutputStream()) {
            workbook.write(outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // 关闭工作簿
        workbook.close();
    }
}
